package com.warrior.superdata.mysql.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.warrior.superdata.constants.ResultCode;
import com.warrior.superdata.mysql.entity.ApiDatasource;
import com.warrior.superdata.mysql.entity.dto.ApiDatasourceDto;
import com.warrior.superdata.mysql.entity.dto.TableFieldDto;
import com.warrior.superdata.mysql.mapper.ApiDatasourceMapper;
import com.warrior.superdata.mysql.service.ApiDatasourceSerivce;
import com.warrior.superdata.exceptionhandler.ApplicationException;
import com.warrior.superdata.service.impl.QuerySqlServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

/**
 * @Description:
 * @ClassName: ApiDatasourceImpl.java
 * @date: 2022/3/2 18:08
 * @author xujian
 */
@Slf4j
@Service
public class ApiDatasourceImpl extends ServiceImpl<ApiDatasourceMapper, ApiDatasource> implements ApiDatasourceSerivce {

    @Autowired
    private QuerySqlServiceImpl sqlService;

    @Override
    public List<String> testSourceConnect(ApiDatasourceDto apiDatasourceDto) throws ApplicationException {
        return sqlService.testSourceConnect(apiDatasourceDto);
    }

    @Override
    public void addDatasource(ApiDatasourceDto apiDatasourceDto) {
        ApiDatasource apiDatasource = new ApiDatasource();
        BeanUtils.copyProperties(apiDatasourceDto, apiDatasource);
        this.save(apiDatasource);
    }

    @Override
    public List<ApiDatasource> getDatasourceList(Integer type) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (null != type) {
            queryWrapper.eq("type", type);
        }
        return this.list(queryWrapper);
    }

    @Override
    public Set<String> getTables(String datasourceCode, String dataBaseName) {
        if (StringUtils.isBlank(datasourceCode) ) {
            throw new ApplicationException(ResultCode.FAIL_CODE, "datasourceCode不能为空");
        }

        ApiDatasource apiDatesource = checkApiDatasource(datasourceCode);
        ApiDatasourceDto apiDatasourceDto = new ApiDatasourceDto();
        BeanUtils.copyProperties(apiDatesource, apiDatasourceDto);
        apiDatasourceDto.setDatabaseName(dataBaseName);

        return sqlService.getTables(apiDatasourceDto);
    }

    @Override
    public List<String> getDatabases(String datasourceCode) {
        if (StringUtils.isBlank(datasourceCode)) {
            throw new ApplicationException(ResultCode.FAIL_CODE, "datasourceCode不能为空");
        }

        ApiDatasource apiDatesource = checkApiDatasource(datasourceCode);
        ApiDatasourceDto apiDatasourceDto = new ApiDatasourceDto();
        BeanUtils.copyProperties(apiDatesource, apiDatasourceDto);

        return sqlService.testSourceConnect(apiDatasourceDto);
    }



    @Override
    public List<TableFieldDto> getTableFields(String datasourceCode, String dataBaseName, String tableName) {
        if (StringUtils.isBlank(datasourceCode) || StringUtils.isBlank(tableName)) {
            throw new ApplicationException(ResultCode.FAIL_CODE, "datasourceCode,tableName不能为空");
        }
        ApiDatasource apiDatesource = checkApiDatasource(datasourceCode);
        ApiDatasourceDto apiDatasourceDto = new ApiDatasourceDto();
        BeanUtils.copyProperties(apiDatesource, apiDatasourceDto);
        apiDatasourceDto.setDatabaseName(dataBaseName);
        return sqlService.getTableFields(apiDatasourceDto, tableName);
    }

    @Override
    public ApiDatasource checkApiDatasource(String datasourceCode) {
        QueryWrapper<ApiDatasource> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("datasource_code", datasourceCode);
        ApiDatasource apiDatesource = this.getOne(queryWrapper);
        if (null == apiDatesource) {
            throw new ApplicationException(ResultCode.FAIL_CODE, "datasourceCode输入有误");
        }
        return apiDatesource;
    }

}
